import {Route, Redirect} from 'react-router-dom';

const SecuredRoute = ({path}) => {

  const {checkSession, checkPrivilege} = useContext(SessionContext);

  const render = () => {
    if (!checkSession()) {
      return <Redirect to='/log-reg' />
    } else if (path === '/log-reg'){
      return <Redirect to='/dash-board' />
    } else if (!checkPrivilege()){
      return <Redirect to='/lack-privilege' />
    } else {
      return <Page path={path} />
    }
  }

  return <Route path={path} render={render} />
}

export default SecuredRoute;